/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import hibernate.entity.Account;
import hibernate.entity.Feedback;
import hibernate.entity.TrackedProduct;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Projections;

/**
 *
 * @author Tùng
 */
public class FeedbackDAO {

    private Session session;

    public FeedbackDAO() {
        SessionFactory sf = Common.HibernateUtil.getSessionFactory();
        session = sf.getCurrentSession();
    }

    public List<Feedback> loadFeedback() {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "from Feedback";
            Query query = session.createQuery(sql);
            List<Feedback> result = query.list();
            session.getTransaction().commit();
            return result;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return null;
    }

    public boolean createFeedback(int accountID, String feedbackBody) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Account acc = (Account) session.get(Account.class, accountID);
            DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            Date date = new Date();
            String createdDate = dateFormat.format(date);
            Feedback feed = new Feedback(acc, feedbackBody, createdDate);
            session.save(feed);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteFeedback(int feedbackID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Feedback fb = (Feedback) session.get(Feedback.class, feedbackID);
            session.delete(fb);
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public boolean deleteFeedbackbyAccountID(int accountID) {
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            String sql = "delete Feedback where AccountID = ?";
            Query query = session.createQuery(sql);
            query.setInteger(0, accountID);
            query.executeUpdate();
            session.flush();
            session.getTransaction().commit();
            return true;
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
            return false;
        }
    }

    public int countFeedback() {
        int result = 0;
        try {
            SessionFactory sf = Common.HibernateUtil.getSessionFactory();
            session = sf.openSession();
            session.getTransaction().begin();
            Number number = (Number) session.createCriteria(Feedback.class).setProjection(Projections.rowCount()).uniqueResult();
            result = number.intValue();
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session.getTransaction().isActive()) {
                session.getTransaction().rollback();
            }
            e.printStackTrace();
        }
        return result;
    }
}
